<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="specificationAndModel">
        <el-input
          v-model="queryParams.specificationAndModel"
          placeholder="请输入规格型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产日期">
        <el-date-picker
          v-model="daterangeProductionDate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
<!--      <el-form-item label="探测仪编号" prop="code">-->
<!--        <el-input-->
<!--          v-model="queryParams.code"-->
<!--          placeholder="请输入探测仪编号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="安装(放置)位置" prop="installationLocation">-->
<!--        <el-input-->
<!--          v-model="queryParams.installationLocation"-->
<!--          placeholder="请输入安装(放置)位置"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="报警浓度(%LEL)" prop="alarmConcentration">-->
<!--        <el-input-->
<!--          v-model="queryParams.alarmConcentration"-->
<!--          placeholder="请输入报警浓度(%LEL)"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="检定日期" prop="verificationDate">-->
<!--        <el-date-picker clearable size="small"-->
<!--          v-model="queryParams.verificationDate"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择检定日期">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="检定结果" prop="verificationResults">-->
<!--        <el-select v-model="queryParams.verificationResults" placeholder="请选择检定结果" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in verificationResultsOptions"-->
<!--            :key="dict.dictValue"-->
<!--            :label="dict.dictLabel"-->
<!--            :value="dict.dictValue"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="检定有效期" prop="verificationExpirationDate">-->
<!--        <el-date-picker clearable size="small"-->
<!--          v-model="queryParams.verificationExpirationDate"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择检定有效期">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用" prop="remarkSpare">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare"-->
<!--          placeholder="请输入备用"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用1" prop="remarkSpare1">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare1"-->
<!--          placeholder="请输入备用1"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用2" prop="remarkSpare2">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare2"-->
<!--          placeholder="请输入备用2"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ledger:detector:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ledger:detector:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ledger:detector:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['ledger:detector:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detectorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="规格型号" align="center" prop="specificationAndModel" />
      <el-table-column label="生产日期" align="center" prop="productionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="探测仪编号" align="center" prop="code" />
      <el-table-column label="安装(放置)位置" align="center" prop="installationLocation" />
      <el-table-column label="报警浓度(%LEL)" align="center" prop="alarmConcentration" />
      <el-table-column label="检定日期" align="center" prop="verificationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.verificationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检定结果" align="center" prop="verificationResults" :formatter="verificationResultsFormat" />
      <el-table-column label="检定有效期" align="center" prop="verificationExpirationDate">
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
<!--      <el-table-column label="备用" align="center" prop="remarkSpare" />-->
<!--      <el-table-column label="备用1" align="center" prop="remarkSpare1" />-->
<!--      <el-table-column label="备用2" align="center" prop="remarkSpare2" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ledger:detector:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ledger:detector:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改可燃气体浓度探测器台账对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="规格型号" prop="specificationAndModel">
          <el-input v-model="form.specificationAndModel" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="探测仪编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入探测仪编号" />
        </el-form-item>
        <el-form-item label="安装(放置)位置" prop="installationLocation">
          <el-input v-model="form.installationLocation" placeholder="请输入安装(放置)位置" />
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="生产日期" prop="productionDate">
          <el-date-picker clearable size="small"
                          v-model="form.productionDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择生产日期"  style="width: 100%">
          </el-date-picker>
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="报警浓度(%LEL)" prop="alarmConcentration">
          <el-input-number v-model="form.alarmConcentration" placeholder="请输入报警浓度(%LEL)"  style="width: 100%"/>
        </el-form-item>
          </el-col>
        </el-row>
          <el-row :gutter="20">
            <el-col :span="12" :xs="24">
        <el-form-item label="检定日期" prop="verificationDate">
          <el-date-picker clearable size="small"
            v-model="form.verificationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择检定日期"  style="width: 100%">
          </el-date-picker>
        </el-form-item>
            </el-col>
            <el-col :span="12" :xs="24">
        <el-form-item label="检定结果" prop="verificationResults">
          <el-select v-model="form.verificationResults" placeholder="请选择检定结果"  style="width: 100%">
            <el-option
              v-for="dict in verificationResultsOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
            </el-col>
          </el-row>
        <el-form-item label="检定有效期" prop="verificationExpirationDate">
          <el-input v-model="form.verificationExpirationDate" placeholder="请输入检定有效期" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
<!--        <el-form-item label="备用" prop="remarkSpare">-->
<!--          <el-input v-model="form.remarkSpare" placeholder="请输入备用" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用1" prop="remarkSpare1">-->
<!--          <el-input v-model="form.remarkSpare1" placeholder="请输入备用1" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用2" prop="remarkSpare2">-->
<!--          <el-input v-model="form.remarkSpare2" placeholder="请输入备用2" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDetector, getDetector, delDetector, addDetector, updateDetector, exportDetector } from "@/api/ledger/detector";

export default {
  name: "Detector",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 可燃气体浓度探测器台账表格数据
      detectorList: [],
      daterangeProductionDate: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 检定结果字典
      verificationResultsOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        specificationAndModel: null,
        productionDate: null,
        code: null,
        installationLocation: null,
        alarmConcentration: null,
        verificationDate: null,
        verificationResults: null,
        verificationExpirationDate: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        specificationAndModel: [
          { required: true, message: "规格型号不能为空", trigger: "blur" }
        ],
        productionDate: [
          { required: true, message: "生产日期不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "探测仪编号不能为空", trigger: "blur" }
        ],
        installationLocation: [
          { required: true, message: "安装(放置)位置不能为空", trigger: "blur" }
        ],
        alarmConcentration: [
          { required: true, message: "报警浓度(%LEL)不能为空", trigger: "blur" }
        ],
        verificationDate: [
          { required: true, message: "检定日期不能为空", trigger: "blur" }
        ],
        verificationResults: [
          { required: true, message: "检定结果不能为空", trigger: "blur" }
        ],
        verificationExpirationDate: [
          { required: true, message: "检定有效期不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("verification_results").then(response => {
      this.verificationResultsOptions = response.data;
    });
  },
  methods: {
    /** 查询可燃气体浓度探测器台账列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeProductionDate && '' != this.daterangeProductionDate) {
        this.queryParams.params["beginProductionDate"] = this.daterangeProductionDate[0];
        this.queryParams.params["endProductionDate"] = this.daterangeProductionDate[1];
      }
      listDetector(this.queryParams).then(response => {
        this.detectorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 检定结果字典翻译
    verificationResultsFormat(row, column) {
      return this.selectDictLabel(this.verificationResultsOptions, row.verificationResults);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        specificationAndModel: null,
        productionDate: null,
        code: null,
        installationLocation: null,
        alarmConcentration: null,
        verificationDate: null,
        verificationResults: null,
        verificationExpirationDate: null,
        remark: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeProductionDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加可燃气体浓度探测器台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDetector(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改可燃气体浓度探测器台账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDetector(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetector(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除可燃气体浓度探测器台账编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDetector(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有可燃气体浓度探测器台账数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          // return exportDetector(queryParams);
        this.download('/ledger/detector/export', {
          ...this.queryParams
        }, `可燃气体浓度探测器台账.xlsx`)
        this.exportLoading = false;
        }).then(response => {
          // this.download(response.msg);
          // this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
